Simplify Path

Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

题目大意:给定unix路径,返回简化后的路径

题目难度:Medium

/**
 * Created by gzdaijie on 16/5/30
 */
public class Solution {
    public String simplifyPath(String path) {
        String[] strs = path.split("/");
        int k = 0;
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].equals("")) continue;
            if (strs[i].equals(".")) continue;
            if (strs[i].equals("..")) {
              if (k > 0) k--;
            }
            else strs[k++] = strs[i];
        }

        String result = "";
        for (int i = 0; i < k ; i++) {
            result += "/" + strs[i];
        }
        return result.equals("") ? "/" : result;
    }
}
gzdaijie            updated 2016-05-30 22:21:23

results matching ""

    No results matching ""